package com.share.config.wechat.pay;

import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderRequest;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.WxPayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class WxPay {
    @Autowired
    private WxPayProperties properties;

    @Autowired
    private WxPayService wxPayService;

    /**
     * 支付请求信息封装
     *
     * @param openId
     * @param tradeNo
     * @param body
     * @param ip
     * @param fee
     * @return
     */
    public WxPayUnifiedOrderRequest create(String openId, String tradeNo, String body, String ip, Integer fee) {
        WxPayUnifiedOrderRequest req = new WxPayUnifiedOrderRequest();
        req.setOpenid(openId);
        req.setOutTradeNo(tradeNo);
        req.setOutTradeNo("JSAPI");
        req.setBody(body);
        req.setTotalFee(fee);
        req.setFeeType("CNY");
        req.setSpbillCreateIp(ip);
        req.setNotifyUrl(properties.getNotifyUrl());
        return req;
    }

    /**
     * 支付调用
     *
     * @param openId
     * @param tradeNo
     * @param body
     * @param ip
     * @param fee
     * @throws WxPayException
     */
    public Object pay(String openId, String tradeNo, String body, String ip, Integer fee) throws WxPayException {
        WxPayUnifiedOrderRequest req = create(openId, tradeNo, body, ip, fee);
        Object order = wxPayService.createOrder(req);
        return order;
    }
}
